﻿Imports Aspose.Words
Imports System.IO
Imports System.IO.Path
Imports System.Drawing.Printing
Imports System.Data

Partial Class Page_PrintLabel_Viewer
    Inherits System.Web.UI.Page
    Private strPrintName As String = ""

    Protected Sub btPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btPrint.Click
        Try
            Dim applicationPath As String = Server.MapPath(Request.ApplicationPath)
            If Not applicationPath.EndsWith("\") Then
                applicationPath = applicationPath + "\"
            End If
            Dim licenseFile As String = Path.Combine(applicationPath, "bin\Aspose.Words.lic")
            If (File.Exists(licenseFile)) Then
                Dim license As Aspose.Words.License = New Aspose.Words.License()
                license.SetLicense(licenseFile)
                Dim strDocFile As String = Request("swf").Replace("../../", "").Replace(".swf", ".doc").Replace("/", "\")
                strDocFile = Path.Combine(applicationPath, strDocFile)
                Dim doc As Aspose.Words.Document = New Aspose.Words.Document(strDocFile)
                Dim strImgFile As String = strDocFile.Replace(".doc", ".png")
                doc.Save(strImgFile, SaveFormat.Png)
                imgLabel.Src = Request("swf").Replace(".swf", ".png")
                'doc.Print(strPrintName)
                If Not IsNothing(doc) Then
                    doc = Nothing
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Call PopulateInstalledPrintersCombo()
            Call PopulateFileTypeCombo()
            Call convert2Picture()
        End If
    End Sub

    Private Sub PopulateFileTypeCombo()
        comboFileType.Items.Add("Word")
        comboFileType.Items.Add("pdf")
    End Sub

    Private Sub PopulateInstalledPrintersCombo()
        Dim i As Integer
        Dim pkInstalledPrinters As String = ""
        For i = 0 To PrinterSettings.InstalledPrinters.Count - 1
            pkInstalledPrinters = PrinterSettings.InstalledPrinters.Item(i)
            comboInstalledPrinters.Items.Add(pkInstalledPrinters)
        Next
    End Sub

    Protected Sub comboInstalledPrinters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboInstalledPrinters.SelectedIndexChanged
        Try
            If comboInstalledPrinters.SelectedIndex <> -1 Then
                strPrintName = comboInstalledPrinters.Text
            End If
        Catch ex As Exception
        End Try
    End Sub

   
    Protected Sub btExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btExport.Click
        Try
            Dim applicationPath As String = Server.MapPath(Request.ApplicationPath)
            If Not applicationPath.EndsWith("\") Then
                applicationPath = applicationPath + "\"
            End If
            Dim strDocFile As String = Request("swf").Replace("../../", "").Replace(".swf", ".doc").Replace("/", "\")
            strDocFile = Path.Combine(applicationPath, strDocFile)
            Select Case comboFileType.Text.Trim.ToLower
                Case "word"
                    Call exportFile(strDocFile)
                Case "pdf"
                    Dim strPdfFile As String = strDocFile.Replace(".doc", ".pdf")
                    If Not File.Exists(strPdfFile) Then
                        Dim licenseFile As String = Path.Combine(applicationPath, "bin\Aspose.Words.lic")
                        If (File.Exists(licenseFile)) Then
                            Dim license As Aspose.Words.License = New Aspose.Words.License()
                            license.SetLicense(licenseFile)
                            Dim doc As Aspose.Words.Document = New Aspose.Words.Document(strDocFile)
                            doc.Save(strPdfFile, SaveFormat.Pdf)
                            If Not IsNothing(doc) Then
                                doc = Nothing
                            End If
                        End If
                    End If
                    Call exportFile(strPdfFile)
            End Select
        Catch ex As Exception

        End Try
    End Sub

    Private Sub exportFile(ByVal FileName As String)
        Try
            Dim strFileFullName As String = ""
            Dim strFileExt As String = ""
            Dim strFileContentType As String = ""
            Dim objFileInfor As FileInfo
            If File.Exists(FileName) Then
                objFileInfor = New FileInfo(FileName)
                If objFileInfor.Exists Then

                    strFileExt = Replace(LCase(objFileInfor.Extension), ".", "")
                    Select Case strFileExt
                        Case "pdf"
                            strFileContentType = "application/pdf"
                        Case "doc", "docx"
                            strFileContentType = "application/msword"
                        Case Else
                            strFileContentType = "application/x-application"
                    End Select

                    ' Clear the current output content from the buffer
                    Response.Clear()

                    ' Add the header that specifies the default filename 
                    ' for the Download/SaveAs dialog
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & objFileInfor.Name)

                    ' Add the header that specifies the file size, so that the 
                    ' browser can show the download progress
                    Response.AddHeader("Content-Length", objFileInfor.Length.ToString())

                    ' Specify that the response is a stream that cannot be read _
                    ' by the client and must be downloaded
                    Response.ContentType = strFileContentType

                    ' Send the file stream to the client
                    Response.WriteFile(objFileInfor.FullName)

                    ' Release objects
                    objFileInfor = Nothing
                    'tblTemp = Nothing

                    ' Stop the execution of this page
                    Response.End()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub convert2Picture()
        Try
            Dim applicationPath As String = Server.MapPath(Request.ApplicationPath)
            If Not applicationPath.EndsWith("\") Then
                applicationPath = applicationPath + "\"
            End If
            Dim licenseFile As String = Path.Combine(applicationPath, "bin\Aspose.Words.lic")
            If (File.Exists(licenseFile)) Then
                Dim license As Aspose.Words.License = New Aspose.Words.License()
                license.SetLicense(licenseFile)
                Dim strDocFile As String = Request("swf").Replace("../../", "").Replace(".swf", ".doc").Replace("/", "\")
                strDocFile = Path.Combine(applicationPath, strDocFile)
                Dim doc As Aspose.Words.Document = New Aspose.Words.Document(strDocFile)
                Dim strImgFile As String = strDocFile.Replace(".doc", ".png")
                doc.Save(strImgFile, SaveFormat.Png)
                imgLabel.Src = Request("swf").Replace(".swf", ".png")
                If Not IsNothing(doc) Then
                    doc = Nothing
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class
